package com.huike.stock_analysis.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.List;

import javax.annotation.Resource;


import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.huike.stock_analysis.entity.DebtInfo;
import com.huike.stock_analysis.entity.GrowInfo;
import com.huike.stock_analysis.entity.OperationInfo;
import com.huike.stock_analysis.entity.ProfitInfo;

/**
 * 
 * @author mzLEI
 * @date 2020年7月2日
 */
@Repository
public class FinanceDao {
	
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	
	
	/**
	 * 
	 * @param code
	 * @return List<ProfitInfo> 一只股票的所有盈利能力的数据 按照日期升序排序
	 *   查询盈利能力数据
	 */
	public List<ProfitInfo> queryProfit(String code) {
		//定义sql
		String sql = "SELECT * FROM `ads_ylnl` WHERE `ads_id` = " + code + " ORDER BY `ads_date`";
		
		
		
		/*查询
		 *profitInfos保存查询到的数据 每一条数据封装到一个ProfitInfo对象
		 */
		List<ProfitInfo> profitInfos = jdbcTemplate.query(sql,  new RowMapper<ProfitInfo>() {
			
			@Override
			public ProfitInfo mapRow(ResultSet rs, int col) throws SQLException {
				ProfitInfo profitInfo = new ProfitInfo();
				profitInfo.setProfitRatio(rs.getDouble("ads_yylrl"));
				profitInfo.setNetAssetsIncomRatio(rs.getDouble("ads_jzcsyl"));
				profitInfo.setNetAssetsIncomRatioOfSame(rs.getDouble("ads_hypjjzcsyl"));
				profitInfo.setNetProfit(rs.getDouble("ads_jlr"));
				profitInfo.setMainBusinessIncome(rs.getDouble("ads_zyywsr"));
				profitInfo.setPerShareProfit(rs.getDouble("ads_mgsy"));
				profitInfo.setDate(rs.getDate("ads_date"));
				//System.out.print(rs.getDate("ads_date"));
				profitInfo.setNetProfitOfSame(rs.getDouble("ads_hypjjlr"));
				profitInfo.setMainBusinessIncomeOfSame(rs.getDouble("ads_hypjzyywsr"));
				profitInfo.setPerShareProfitOfSame(rs.getDouble("ads_hypjmgsy"));
				profitInfo.setProfitRatioOfSame(rs.getDouble("ads_hypjyylrl"));
				profitInfo.setGrossProfitRatio(rs.getDouble("ads_xsmll"));
				profitInfo.setGrossNetProfitRatio(rs.getDouble("ads_xsjll"));
				profitInfo.setPerNetAssets(rs.getDouble("ads_mgjzc"));
				
				profitInfo.setLeftCashTime(rs.getDouble("ads_yyxjbzbs"));
				profitInfo.setCostProfitRatio(rs.getDouble("ads_cbfylrl"));
				return profitInfo;
			}
		});
		
		return profitInfos;
	}
		
		/**
		 * 
		 * @param code
		 * @return List<GrowInfo> 一只股票的所有成长能力的数据 按照日期升序排序
		  *  成长能力数据
		 */
		public List<GrowInfo> queryGrow(String code) {
			//定义查询sql
			String sql = "SELECT * FROM `ads_chengzhangnengli` WHERE `ads_id` = " + code + " ORDER BY `ads_date`";
			
			/*查询
			 *growInfos保存查询到的数据 每一条数据封装到一个ProfitInfo对象
			 */
			List<GrowInfo> growInfos = jdbcTemplate.query(sql, new RowMapper<GrowInfo>() {
				@Override
				public GrowInfo mapRow(ResultSet rs,  int col) throws SQLException {
					GrowInfo growInfo = new GrowInfo();
					
					growInfo.setNetProfitGrowRatio(rs.getDouble("ads_jlrzzl"));
					growInfo.setNetAssetsGrowRatio(rs.getDouble("ads_jzczzl"));
					growInfo.setMainBusinessGrowRatio(rs.getDouble("ads_zyywsrzzl"));
					growInfo.setTotalAssetsGrowRatio(rs.getDouble("ads_zzczzl"));
					growInfo.setMainProfitGrowRatio(rs.getDouble("ads_zylrzzl"));
					growInfo.setDate(rs.getDate("ads_date"));
					growInfo.setAvrMainBusinessGrowRatioOfSame(rs.getDouble("ads_hypjzyywsrzzl"));
					growInfo.setAvrNetProfitGrowRatioOfSame(rs.getDouble("ads_hypjjlrzzl"));
					growInfo.setFixesAssetsRatio(rs.getDouble("ads_gdzcbz"));
					growInfo.setTotalAssetsGrowRatioOfSame(rs.getDouble("ads_hypjzzczzl"));
					return growInfo;
				}
			});
			
			return growInfos;
		
	}
		
		/**
		 * 
		 * @param code
		 * @return List<DebtInfo> 一只股票的所有偿债能力的数据 按照日期升序排序
		  *  偿债能力数据
		 */
		public List<DebtInfo> queryDebt(String code) {
			//定义查询sql
			String sql = "SELECT * FROM `ads_changzhainengli` WHERE `ads_id` = " + code + " ORDER BY `ads_date`";
			
			/*查询
			 *debtInfos保存查询到的数据 每一条数据封装到一个ProfitInfo对象
			 */
			List<DebtInfo> debtInfos = jdbcTemplate.query(sql, new RowMapper<DebtInfo>() {
				@Override
				public DebtInfo mapRow(ResultSet rs,  int col) throws SQLException {
					DebtInfo debtInfo = new DebtInfo();
					debtInfo.setDate(rs.getDate("ads_date"));
					debtInfo.setAssetsDebtRatio(rs.getDouble("ads_zcfzl"));
					debtInfo.setAssetsDebtRatioOfSame(rs.getDouble("ads_hypjzcfzl"));
					debtInfo.setFlowRatio(rs.getDouble("ads_ldbl"));
					debtInfo.setFlowRatioOfSame(rs.getDouble("ads_hypjldbl"));
					debtInfo.setSpendRatio(rs.getDouble("ads_sdbl"));
					debtInfo.setEquityRatio(rs.getDouble("ads_cqbl"));
					debtInfo.setLongTermDebtRatio(rs.getDouble("ads_cqfzbl"));
					debtInfo.setFixedAssetsRaito(rs.getDouble("ads_zbgdhbl"));
					debtInfo.setCashRatio(rs.getDouble("ads_xjbl"));
					
					return debtInfo;
				}
			});
			
			return debtInfos;
		
	}
	
		
		/**
		 * 
		 * @param code
		 * @return List<Operation> 一只股票的所有运营能力的数据 按照日期升序排序
		  *  运营能力数据
		 */
		public List<OperationInfo> queryOperation(String code) {
			//定义查询sql
			String sql = "SELECT * FROM `ads_yynl` WHERE `ads_id` = " + code + " ORDER BY `ads_date`";
			
			/*查询
			 *operationInfos保存查询到的数据 每一条数据封装到一个ProfitInfo对象
			 */
			List<OperationInfo> operationInfos = jdbcTemplate.query(sql, new RowMapper<OperationInfo>() {
				@Override
				public OperationInfo mapRow(ResultSet rs,  int col) throws SQLException {
					OperationInfo operationInfo = new OperationInfo();
					operationInfo.setInventoryTurnoverRatio(rs.getDouble("ads_chzzl"));
					operationInfo.setInventoryTurnoverRatioOfSame(rs.getDouble("ads_hypjchzzl"));
					operationInfo.setAccountsReceivableTurnoverRatio(rs.getDouble("ads_yszzzl"));
					operationInfo.setDate(rs.getDate("ads_date"));
					operationInfo.setFixCapitalTurnoverRatio(rs.getDouble("ads_gdzjzzl"));
					operationInfo.setFlowCapitalTurnoverRatio(rs.getDouble("ads_ldzjzzl"));
					operationInfo.setTotalAssetsTurnoverRatio(rs.getDouble("ads_zzczzl"));
					operationInfo.setTotalAssetsTurnoverRatioOfSame(rs.getDouble("ads_hypjzzczzl"));
					operationInfo.setCashRatio(rs.getDouble("ads_xjllbl"));
					return operationInfo;
				}
			});
			
			return operationInfos;
		
	}
	
	

}
